<script setup>
import { ref, onMounted } from 'vue'
import { getAllInterviews } from '@/api/admin'
import { convertStatusToNumber } from '@/utils/enum'

const searchForm = ref({ page: 1, size: 10 })
const interviews = ref([])
const total = ref(0)
const loading = ref(false)
const statusMap = {
  PENDING_CONFIRM: '待确认',
  ACCEPTED: '已接受',
  REJECTED: '已拒绝'
}
const statusTagType = status => {
  const numStatus = convertStatusToNumber(status, 'interview')
  if (numStatus === 0) return 'info'
  if (numStatus === 1) return 'success'
  if (numStatus === 2) return 'danger'
  return ''
}
const allInterviews = ref([])
const fetchInterviews = async () => {
  loading.value = true
  try {
    const res = await getAllInterviews({})
    const data = res.data || res
    allInterviews.value = data.list || []
    total.value = allInterviews.value.length
    const start = (searchForm.value.page - 1) * searchForm.value.size
    const end = start + searchForm.value.size
    interviews.value = allInterviews.value.slice(start, end)
  } catch (e) {}
  loading.value = false
}
const onPageChange = page => {
  searchForm.value.page = page
  const start = (searchForm.value.page - 1) * searchForm.value.size
  const end = start + searchForm.value.size
  interviews.value = allInterviews.value.slice(start, end)
}
onMounted(() => {
  fetchInterviews()
})
</script>

<template>
  <el-card class="admin-interview-list-card">
    <h2>全平台面试邀请管理</h2>
    <el-table :data="interviews" stripe border class="interview-table" v-loading="loading">
      <el-table-column prop="position_name" label="岗位名称" min-width="120" />
      <el-table-column prop="enterprise_name" label="企业名称" min-width="120" />
      <el-table-column prop="studentName" label="学生姓名" min-width="100" />
      <el-table-column prop="interview_time" label="面试时间" min-width="140" />
      <el-table-column prop="interview_location" label="面试地点" min-width="120" />
      <el-table-column prop="status" label="状态" min-width="100">
        <template #default="{ row }">
          <el-tag :type="statusTagType(row.status)">{{ statusMap[row.status] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" min-width="120" />
    </el-table>
    <div class="pagination-bar">
      <el-pagination
          background
          layout="prev, pager, next, jumper"
          :total="total"
          :page-size="searchForm.size"
          :current-page="searchForm.page"
          @current-change="onPageChange"
      />
    </div>
  </el-card>
</template>
